﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Collections;
using System.Text;

namespace Catering.View_Operaction
{
    public class DataBase_Manage_Operaction
    {
        #region 出库管理
        public string InputDataBase(string strInfo, string strRowID)
        {
            string[] infos = strInfo.Split(new char[] { ',', ' ' });
            string str = string.Empty;
            if (infos.Length > 0)
            {
                Int32 intid = GetDataBaseNumber(infos[0], infos[1], infos[2], infos[3], infos[4], infos[5], infos[6], infos[7], infos[8], infos[9], infos[10], strRowID);
                if (intid > 0)
                {

                    Catering_Model.FromRecords cmfr = new Catering_Rules.FromRecords().GetModel(Convert.ToInt32(intid));
                    str = ShowInputRoom(cmfr);
                }

            }
            return str;
        }
        public Boolean InputDataBase(string strInfo)
        {
            string[] infos = strInfo.Split(new char[] { ',', ' ' });
            Boolean blrest = false;
            if (infos.Length > 0)
            {
                DateTime dtime = new DateTime();
                Boolean bl = DateTime.TryParse(infos[4], out  dtime);
                DataSet ds = new Catering_Rules.Monitor().GetAllList();
                if (ds != null && ds.Tables.Count > 0)
                {
                    if (ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
                    {
                        if (bl)
                        {

                            var info = from inf in ds.Tables[0].AsEnumerable()
                                       where inf.Field<string>("OutDate").ToString().Substring(0, inf.Field<string>("OutDate").LastIndexOf("/"))
                                           .Equals(dtime.ToShortDateString().Substring(0, dtime.ToShortDateString().LastIndexOf("/")))
                                       where inf.Field<string>("OutState").Equals("1")
                                       select inf;
                            if (info.Count() < 1)
                            {
                                blrest = true;
                            }

                        }
                    }
                    else
                    {
                        blrest = true;
                    }

                }
                else
                {
                    blrest = true;
                }


            }
            return blrest;
        }

        private string ShowInputRoom(Catering_Model.FromRecords cmfr)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();


            sb.Append("<tr id=\"InputID" + cmfr.id + "\">");
            sb.Append("<td>" + 1 + "</td>");
            sb.Append("<td>" + cmfr.RAWID + "</td>");
            sb.Append("<td>" + cmfr.RAWIDs + "</td>");
            sb.Append("<td>" + cmfr.RAWID + "</td>");
            sb.Append("<td>" + cmfr.RAWName + "</td>");
            sb.Append("<td>" + cmfr.Price + "</td>");
            sb.Append("<td>" + cmfr.Numbers + "</td>");
            sb.Append("<td>" + cmfr.AmountMoney + "</td>");
            sb.Append("<td>" + cmfr.CMP + "</td>");
            sb.Append("<td>" + cmfr.OutDate.ToShortDateString() + "</td>");
            sb.Append("<td>" + cmfr.DBID + "</td>");
            sb.Append("<td>" + cmfr.Back + "</td>");
            sb.Append("<td>");
            sb.Append("<input type=\"button\" name=\"edit" + cmfr.id + "\" value=\"编辑\" onclick=\"update_input_room_info(" + cmfr.id + ")\" /> ");
            sb.Append("<input type=\"button\" name=\"delete" + cmfr.id + "\" value=\"删除\" onclick=\"Del_input_room_info(" + cmfr.id + ")\" /></td>");
            sb.Append("</tr>");
            return sb.ToString();
        }
        private Int32 GetDataBaseNumber(string strGroupID, string strClassID, string strDBID, string strRawID, string strOutDate, string strNumber, string strBack, string strMoney, string strCMP, string strsp, string strzongjine, string RowID)
        {

            Catering_Rules.ViewOperaction vo = new Catering_Rules.ViewOperaction();

            Catering_Model.FromRecords cmfr = null;
            Catering_Rules.FromRecords crfr = new Catering_Rules.FromRecords();
            Int32 intID = 0, rawid = 0;

            string str = "GroupID= " + strGroupID + " and ClassID= " + strClassID;
            if (strRawID != null && strRawID.Length > 0)
            {
                rawid = new Catering_Rules.ViewOperaction().GetSingle("RawMaterial_Area", "RawID", "InDBid='" + strRawID + "'");
                str = str + "and RAWID= " + rawid;

            }


            cmfr = new Catering_Model.FromRecords();
            cmfr.GroupID = Convert.ToInt32(strGroupID);
            cmfr.ClassID = Convert.ToInt32(strClassID);
            cmfr.DBID = 1;
            Catering_Model.E_RawMaterial cmrm = new Catering_Rules.T_RawMaterial().GetModel(rawid);
            if (cmrm != null)
            {
                cmfr.RAWTID = cmrm.RAWID;
                cmfr.RAWIDs = cmrm.RAWIDS;
                cmfr.RAWID = cmrm.id;
                cmfr.RAWName = cmrm.RAWName;
            }

            cmfr.Numbers = strNumber;
            cmfr.AmountMoney = Convert.ToDecimal(strMoney) * Convert.ToDecimal(strNumber);
            cmfr.CMP = strCMP;
            cmfr.Specifications = strsp;
            cmfr.Price = Convert.ToDecimal(strMoney);
            cmfr.OutDate = Convert.ToDateTime(strOutDate);
            cmfr.Back = strBack;
            cmfr.temp1 = "";
            cmfr.temp2 = "";
            cmfr.InDBid = strRawID;
            Int32 tempId = 0;
            Boolean bl = Int32.TryParse(RowID, out tempId);
            if (!bl)
            {


                if (Reduction_Raw_RoomNumber(Convert.ToInt32(strGroupID), Convert.ToInt32(strClassID), 1, rawid, Convert.ToDecimal(strNumber)))
                {
                    intID = crfr.GetMaxId();
                    cmfr.id = intID;
                    crfr.Add(cmfr);
                }

            }
            else
            {
                intID = tempId;
                cmfr.id = tempId;

                crfr.Update(cmfr);

            }
            return intID;
        }




        private Boolean Reduction_Raw_RoomNumber(Int32 GroupID, Int32 ClassID, Int32 DBID, Int32 RawID, Decimal OutNumber)
        {
            Boolean bl = false;
            DataSet ds = new Catering_Rules.InDbInfo().GetAllList();
            if (ds != null && ds.Tables.Count > 0)
            {
                if (ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
                {
                    //var RawInfo = from inof in ds.Tables[0].AsEnumerable()
                    //              where inof.Field<Int32>("GroupID") == GroupID
                    //              where inof.Field<Int32>("ClassID") == ClassID
                    //              where inof.Field<Int32>("DBID") == DBID
                    //              where inof.Field<Int32>("RAWID") == RawID
                    //              select inof;
                    //DataRow dr = RawInfo.Max<DataRow>() as DataRow;

                    //                    if (dr != null && dr["id"] != null)
                    //                    {
                    //                      Catering_Model.InDbInfo cmii = new Catering_Rules.InDbInfo().GetModel(Convert.ToInt32(dr["id"].ToString().Length>0?dr["id"].ToString():"1"));

                    //                      cmii.temp1 =( Convert.ToDecimal(cmii.temp1 != null && cmii.temp1.Trim().Length > 0 ? cmii.temp1 : "0") - OutNumber).ToString();
                    //                     bl =  new Catering_Rules.InDbInfo().Update(cmii);
                    //}
                    DataRow[] drs = ds.Tables[0].Select("GroupID= " + GroupID + "  and ClassID= " + ClassID + " and  DBID =" + DBID + " and RAWID= " + RawID);
                    if (drs.Length > 0)
                    {
                        Catering_Model.InDbInfo cmii = new Catering_Rules.InDbInfo().GetModel(Convert.ToInt32(drs[drs.Length - 1]["id"].ToString().Length > 0 ? drs[drs.Length - 1]["id"].ToString() : "1"));
                        cmii.temp1 = (Convert.ToDecimal(cmii.temp1 != null && cmii.temp1.Trim().Length > 0 ? cmii.temp1 : "0") - OutNumber).ToString();

                        bl = new Catering_Rules.InDbInfo().Update(cmii);
                    }

                }
            }
            return bl;
        }
        #region 出库信息动态
        public ArrayList GetDataBaseNumber(string strInfo)
        {
            ArrayList list = new ArrayList();

            string[] infos = strInfo.Split(new char[] { ',', ' ' });

            if (infos.Length > 0)
            {

                string str = "GroupID= " + infos[0] + " and ClassID= " + infos[1];
                if (infos[2] != null && infos[2].Length > 0)
                {
                    Int32 RawID = new Catering_Rules.ViewOperaction().GetSingle("RawMaterial_Area", "RawID", "InDBid='" + infos[2] + "'");
                    Catering_Model.E_RawMaterial cmrm = new Catering_Rules.T_RawMaterial().GetModel(RawID);
                    if (cmrm != null)
                    {
                        str = str + "and RAWID= " + cmrm.id;
                        //名称 单价 结余 总金额

                        //名称
                        list.Add(cmrm.RAWName);
                        Double jieyu = 0, jine = 0;

                        Get_input_Raw_Max_Row(Convert.ToInt32(infos[0]), Convert.ToInt32(infos[1]), RawID, infos[2], out jieyu, out jine);
                        //单价
                        //list.Add(jine / jieyu);
                        //单价
                        list.Add(jine);
                        //总金额
                        list.Add(jieyu);

                        //大类
                        list.Add(new Catering_Rules.T_RawMaterials().GetModel(cmrm.RAWID).TypeName);
                        //小类
                        list.Add(new Catering_Rules.RawMaterialsInfo().GetModel(cmrm.RAWIDS).Name);
                        //单位 
                        object rowInfo = cmrm.temp2;
                        list.Add(new Catering_Rules.Units().GetModel(Convert.ToInt32(
                            rowInfo != null && rowInfo.ToString().Trim().Length > 0 ? rowInfo.ToString() : "1")).UName);
                        //规格
                        list.Add(GetSpecifications(RawID.ToString()));
                    }

                }


            }
            return list;
        }
        public string GetSpecifications(string strInDbID)
        {
            string str_Specifications = string.Empty;
            if (strInDbID != null && strInDbID.Trim().Length > 0)
            {

                Catering_Model.E_RawMaterial cmrm = new Catering_Rules.T_RawMaterial().GetModel(Convert.ToInt32(strInDbID));
                if (cmrm != null)
                {
                    DataSet ds_Uit = new Catering_Rules.ViewOperaction().GetViewToDataSet("RawMaterial_Unit_Info", "RawMaterialID=" + cmrm.id);
                    if (ds_Uit != null && ds_Uit.Tables.Count > 0)
                    {
                        if (ds_Uit.Tables[0] != null && ds_Uit.Tables[0].Rows.Count > 0)
                        {
                            foreach (DataRow dr in ds_Uit.Tables[0].Rows)
                            {
                                str_Specifications = str_Specifications + dr["Number"] + dr["UName"] + "|";
                            }
                        }

                    }
                }

            }
            return str_Specifications;
        }
        private void Get_input_Raw_Max_Row(Int32 GroupID, Int32 ClassID, Int32 RawID, string strInDBid, out Double jieyu, out Double danjia)
        {
            Double jie = 0;
            Double zong = 0;
            DataSet ds = new Catering_Rules.InDbInfo().GetAllList();
            if (ds != null && ds.Tables.Count > 0)
            {
                if (ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
                {

                    string str = "GroupID= " + GroupID + " and ClassID= " + ClassID + " and InDBid='" + strInDBid + "'";
                    if (RawID > 0)
                    {

                        str = str + "and RAWID= " + RawID;
                    }
                    DataRow[] drs = ds.Tables[0].Select(str);
                    if (drs != null && drs.Length > 0)
                    {
                        //结余
                        Double.TryParse(drs[drs.Length - 1]["temp1"] != null ? drs[drs.Length - 1]["temp1"].ToString() : "0", out jie);
                        Double.TryParse(drs[drs.Length - 1]["temp2"] != null ? drs[drs.Length - 1]["temp2"].ToString() : "0", out zong);

                    }
                    else
                    {
                        jie = 0;
                        zong = 0;
                    }

                }
            }
            jieyu = jie;
            danjia = zong;
        }
        #endregion
        public string ShowOutputRoom(string strID)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            Catering_Rules.FromRecords crfr = new Catering_Rules.FromRecords();
            Catering_Model.FromRecords cmfr = crfr.GetModel(Convert.ToInt32(strID));

            sb.Append("<tr id=\"UpdateInputID" + cmfr.id + "\">");
            sb.Append("<td>" + 1 + "</td>");
            sb.Append("<td>" + cmfr.RAWTID + "</td>");
            sb.Append("<td>" + cmfr.RAWIDs + "</td>");
            sb.Append("<td>" + cmfr.RAWID + "</td>");
            sb.Append("<td>" + cmfr.RAWName + "</td>");
            sb.Append("<td><input name =\"Price" + strID + "\"type=\"text\" style=\"width:98%\" value=\"" + cmfr.Price + "\" /></td>");
            sb.Append("<td><input name =\"Numbers" + strID + "\"type=\"text\" style=\"width:98%\" value=\"" + cmfr.Numbers + "\" /></td>");
            sb.Append("<td><input name =\"AmountMoney" + strID + "\"type=\"text\" style=\"width:98%\" value=\"" + cmfr.AmountMoney + "\" /></td>");
            sb.Append("<td>" + cmfr.CMP + "</td>");
            sb.Append("<td><input onfocus=\"calendar();\" name =\"OutDate" + strID + "\"type=\"text\" style=\"width:98%\" value=\"" + cmfr.OutDate.ToString("yyyy-mm-dd") + "\" /></td>");
            sb.Append("<td>" + cmfr.DBID + "</td>");
            sb.Append("<td><input name =\"Back" + strID + "\"type=\"text\" style=\"width:98%\" value=\"" + cmfr.Back + "\" /></td>");
            sb.Append("<td>");
            sb.Append("<input type=\"submit\" name=\"edit\" value=\"保存\" onclick=\"update_input_room(" + strID + ")\" /> ");
            sb.Append("<input type=\"submit\" name=\"delete\" value=\"删除\" /></td>");
            sb.Append("</tr>");
            return sb.ToString();
        }

        public Boolean UpdateInputInfo(string strInfo)
        {
            Boolean bl = false;
            string[] strs = strInfo.Split(new char[] { ',', ' ' });
            Catering_Model.FromRecords cmfr = new Catering_Model.FromRecords();
            cmfr.id = Convert.ToInt32(strs[0]);
            cmfr.Price = Convert.ToInt32(strs[1]);
            cmfr.Numbers = strs[2];
            cmfr.OutDate = Convert.ToDateTime(strs[3]);
            cmfr.Back = strs[4];
            bl = new Catering_Rules.FromRecords().Update(cmfr);
            return bl;
        }
        public Boolean DelInputInfo(string strInfo)
        {
            int id = 0;
            Int32.TryParse(strInfo, out id);
            if (id <= 0) return false;
            int result = new Catering_Rules.BalanceInfo().DelFromRecords(id);
            if (result == 3)
            {
                return new Catering_Rules.FromRecords().Delete(id);
            }
            return false;
        }
        public string ShowSeach_Output(string strGroupID, string strClassID, string strDBID, string strSelect, string strSeachContent)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            string str = "GroupID= " + strGroupID + " and ClassID= " + strClassID + " and DBID=" + strDBID;
            if (strSelect != null && strSelect.Length > 0 && strSeachContent != null && strSeachContent.Trim().Length > 0)
            {
                switch (strSelect)
                {
                    case "OutDate":
                        break;
                    case "RAWID":
                        str = str + "  and  RAWID= " + strSeachContent;
                        break;
                    case "RAWName":
                        str = str + "  and  RAWName= '" + strSeachContent + "'";
                        break;
                    case "RAWTID":
                        str = str + "  and  RAWTID= " + strSeachContent;
                        break;
                    case "RAWIDs":
                        str = str + "  and  RAWIDs= " + strSeachContent;
                        break;
                }
                List<Catering_Model.FromRecords> list = new Catering_Rules.FromRecords().GetModelList(str);

                foreach (Catering_Model.FromRecords cmfr in list)
                {
                    sb.Append(ShowInputRoom(cmfr));
                }
            }
            return sb.ToString();
        }
        public DataTable GetFromRecords(
            string strGroupID, string strClassID, string strDBID, string strSelect, string strSeachContent)
        {
            DataTable dt = new DataTable();
            View_Operaction_Common voc = new View_Operaction_Common();
            string str = "GroupID= " + strGroupID + " and ClassID= " + strClassID;
            if (strSelect != null && strSelect.Length > 0)
            {
                if (strSeachContent != null && strSeachContent.Trim().Length > 0)
                {
                    switch (strSelect)
                    {
                        case "OutDate":
                            str = str + "  and  OutDate= '" + Convert.ToDateTime(strSeachContent).ToShortDateString() + "'";
                            break;
                        case "RAWID":

                            str = str + "  and  RAWID =" + new Catering_Rules.ViewOperaction().GetSingle("RawMaterial_Area", "RawID", "InDBid='" + strSeachContent.Trim() + "'");
                            ;
                            break;
                        case "RAWName":
                            str = str + "  and  RAWName like'" + strSeachContent + "%'";
                            break;
                        case "RAWTID":
                            str = str + "  and  RAWTID= " + voc.Get_Raw_MaxType_Id("RawMaterials", "TypeName='" + strSeachContent.Trim() + "'");
                            break;
                        case "RAWIDs":
                            str = str + "  and  RAWIDs= " + voc.Get_Raw_MaxType_Id("RawMaterialsInfo", "[Name] ='" + strSeachContent.Trim() + "'");
                            break;
                    }
                }

                DataSet ds = new Catering_Rules.FromRecords().GetList(str);
                if (ds != null && ds.Tables.Count > 0)
                {
                    if (ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
                    {
                        dt = ds.Tables[0];

                    }
                }
            }
            return dt;


        }
        #endregion
        #region 库房设置
        public string Show_cangkushezhi(string strID)
        {
            StringBuilder sb = new StringBuilder();
            Int32 inti = Convert.ToInt32(strID) + 1;

            sb.Append("<tr id=\"add_" + inti + "\">");
            sb.Append("<td>" + inti + "</td>");
            sb.Append("<td><input id=\"kufang\"  style=\"width:80px\" type=\"text\" value=\"\"/></td>");
            sb.Append("<td><input id=\"riqi\" onfocus=\"calendar();\" style=\"width:80px\" type=\"text\" value=\"" + DateTime.Now.ToShortDateString() + "\"/></td>");
            sb.Append("<td><input id=\"miaoshu\" style=\"width:120px\" type=\"text\" value=\"\"/></td>");
            sb.Append("<td><input type=\"button\" id=\"delete\" name=\"delete\" value=\"删除\"  onclick=\"cangku_Del_info_click()\"/></td>");
            sb.Append("</tr>");

            return sb.ToString();
        }
        public string Save_cangkushezhi(string strRowID)
        {
            string[] infos = strRowID.Split(new char[] { ',', ' ' });
            int i = 0;
            string str = string.Empty;
            if (infos.Length > 0)
            {

                Catering_Rules.StorageRoom crsr = new Catering_Rules.StorageRoom();
                Catering_Model.StorageRoom cmsr = new Catering_Model.StorageRoom();
                i = crsr.GetMaxId();
                cmsr.id = crsr.GetMaxId();
                cmsr.GroupID = Convert.ToInt32(infos[0]);
                cmsr.ClassID = Convert.ToInt32(infos[1]);
                cmsr.RoomID = infos[2];
                cmsr.CreateDate = Convert.ToDateTime(infos[3]);
                cmsr.Description = infos[4];
                cmsr.Adds = "";
                cmsr.DBType = 1;
                cmsr.temp1 = "";
                cmsr.temp2 = "";

                crsr.Add(cmsr);


            }
            return i.ToString();
        }

        #endregion
        #region 杂品申请
        public string add_request_Info(string strInfos)
        {
            Int32 id = Insert_Reqest_Info(strInfos);
            return Show_Insert_Info(new Catering_Rules.SundryGoods().GetModel(id));
        }
        private Int32 Insert_Reqest_Info(string strInfos)
        {
            string[] infos = strInfos.Split(new char[] { ',', ' ' });
            string str = string.Empty;
            Int32 id = 0;

            if (infos.Length > 0)
            {

                Catering_Rules.SundryGoods crsg = new Catering_Rules.SundryGoods();
                Catering_Model.SundryGoods cmsg = new Catering_Model.SundryGoods();
                id = crsg.GetMaxId();
                cmsg.id = id;
                cmsg.GroupID = Convert.ToInt32(infos[0]);
                cmsg.ClassID = Convert.ToInt32(infos[1]);
                cmsg.temp1 = infos[2];

                cmsg.PID = Convert.ToInt32(infos[3]);
                cmsg.Specifications = infos[4];
                cmsg.SName = infos[5];
                cmsg.SType = infos[6];
                cmsg.Number = infos[7];
                cmsg.Description = infos[8];
                cmsg.State = 0;
                cmsg.RAWID = 1;
                crsg.Add(cmsg);


            }
            return id;
        }

        public string Show_Insert_Info(Catering_Model.SundryGoods cmsg)
        {
            StringBuilder sb = new StringBuilder();
            if (cmsg != null)
            {
                sb.Append(" <tr id=\"Info" + cmsg.id + "\">");
                sb.Append(" <td align=\"center\"> " + cmsg.id + "</td>");

                sb.Append(" <td align=\"center\">" + cmsg.ClassID + "</td>");
                sb.Append(" <td align=\"center\">" + cmsg.SType + "</td>");
                sb.Append(" <td align=\"center\">" + cmsg.SName + "</td>");
                sb.Append(" <td align=\"center\"> " + cmsg.Number + "</td>");
                sb.Append(" <td align=\"center\">" + cmsg.Specifications + "</td>");
                sb.Append(" <td align=\"center\">" + cmsg.PID + "</td>");
                if (cmsg.State == 1)
                {
                    sb.Append(" <td align=\"center\">已审批</td>");
                }
                else
                {
                    sb.Append(" <td align=\"center\">未审批</td>");
                }
                sb.Append(" <td align=\"center\"> " + cmsg.Description + "</td>");
                sb.Append("  <td align=\"center\"> <a href=\"#\" onclick=\"but_Updte_stat_Info_click(" + cmsg.id + ")\"> <img src=\"../../Images/file.gif\"  /></a> </td>");

                sb.Append(" </tr>");
            }
            return sb.ToString();
        }
        public string Update_request_Stat(string strInfos)
        {
            if (strInfos != null && strInfos.Length > 0)
            {
                Catering_Model.SundryGoods smsg = new Catering_Rules.SundryGoods().GetModel(Convert.ToInt32(strInfos));
                smsg.id = Convert.ToInt32(strInfos);
                smsg.State = 1;
                if (new Catering_Rules.SundryGoods().Update(smsg))
                {
                    return "OK";
                }
                else
                {
                    return "NO";
                }
            }
            else
            {
                return "NO";
            }
        }
        public DataTable seach_reqest_Info(string strGroupID, string strClassID, string strSelect, string strSeachContent)
        {
            DataTable dt_seacher = new DataTable();
            List<string> list_string = new List<string>();
            list_string.Add("未审批");
            list_string.Add("已审批");
            list_string.Add("不审批");

            string str = "GroupID= " + strGroupID + " and ClassID= " + strClassID;
            if (strSelect != null && strSelect.Length > 0 && strSeachContent != null && strSeachContent.Trim().Length > 0)
            {

                switch (strSelect)
                {
                    case "temp1":
                        str = str + "  and  temp1= '" + strSeachContent + "'";
                        break;
                    case "State":
                        str = str + "  and  State= " + list_string.IndexOf(strSeachContent);
                        break;
                    case "SName":
                        str = str + "  and  SName Like '%" + strSeachContent + "%'";
                        break;
                    case "SType":
                        str = str + "  and  SType= " + strSeachContent;
                        break;
                    case "PID":
                        str = str + "  and  PID= " + strSeachContent;
                        break;
                }

                //List<Catering_Model.SundryGoods> list = new Catering_Rules.SundryGoods().GetModelList(str);

                //foreach (Catering_Model.SundryGoods cmfr in list)
                //{
                //    sb.Append(Show_Insert_Info(cmfr));
                //}
            }
            DataSet ds = new Catering_Rules.SundryGoods().GetList(str);
            if (ds != null && ds.Tables.Count > 0)
            {
                if (ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
                {
                    dt_seacher = ds.Tables[0];
                }
            }
            return dt_seacher;
        }
        #endregion
        #region 仓库盘点
        public string Pandian_Database(string strGroupID, string strClassID, string strDBID, string strMaxType, string strMinType)
        {
            StringBuilder sb = new StringBuilder();
            DataTable dt = Pandian_Database_toDataTable(strGroupID, strClassID, strMaxType, strMinType);
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    sb.Append("<tr>");
                    sb.Append("<td>" + dr["RAWID"] + "</td>");
                    sb.Append(" <td>" + dr["Name"] + "</td>");
                    sb.Append(" <td>" + dr["raw"] + " </td>");
                    sb.Append("<td>" + dr["CMP"] + "</td>");
                    sb.Append("  <td>" + dr["Number"] + "</td>");
                    sb.Append("  <td>" + dr["CMP"] + "</td>");
                    sb.Append(" <td>" + Convert.ToDouble(dr["pice"]) + "</td>");
                    sb.Append("<td>" + Convert.ToDouble(dr["Number"]) * Convert.ToDouble(dr["pice"]) + "</td>");
                    sb.Append(" <td></td>");
                    sb.Append("<td></td>");
                    sb.Append(" <td></td>");
                    sb.Append("</tr>");
                    sb.Append("");


                }
            }
            return sb.ToString();
        }
        private DataTable Pandian_Database_toDataTable(string strGroupID, string strClassID, string strMaxType, string strMinType)
        {
            DataTable dt = new DataTable();
            string strWhere = string.Empty;
            strWhere = "GroupID= " + strGroupID + " and ClassID= " + strClassID;
            if (Convert.ToInt32(strMaxType) > 0)
            {
                strWhere = strWhere + " and rmid=" + strMaxType;
            }
            else if (Convert.ToInt32(strMinType) > 0)
            {
                strWhere = strWhere + " and rmid=" + strMaxType + " and rmiid =" + strMinType;
            }
            DataSet ds = new Catering_Rules.ViewOperaction().GetViewToDataSet("Raw_Pandian", strWhere);
            if (ds != null && ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];


            }
            return dt;
        }

        public DataTable Pandian_Insert_To_Excel(string strGroupID, string strClassID, string strMaxType, string strMinType)
        {
            DataTable dt = new Catering_DataSet.pandianDataTable();
            DataTable dt_pandian = Pandian_Database_toDataTable(strGroupID, strClassID, strMaxType, strMinType);
            int i = 1;
            foreach (DataRow dr in dt_pandian.Rows)
            {
                DataRow drcaigou = dt.NewRow();

                drcaigou["xuhao"] = i;
                drcaigou["leibie"] = dr["Name"].ToString();
                drcaigou["mingcheng"] = dr["raw"].ToString();
                drcaigou["danwei"] = dr["CMP"].ToString();
                drcaigou["shuliang"] = dr["Number"].ToString();
                drcaigou["danweis"] = dr["CMP"].ToString();
                drcaigou["danjia"] = Convert.ToDouble(dr["pice"]);
                drcaigou["jine"] = Convert.ToDouble(dr["Number"]) * Convert.ToDouble(dr["pice"]);
                drcaigou["hege"] = "";
                drcaigou["buhege"] = "";
                drcaigou["beizhu"] = "";

                i++;
                dt.Rows.Add(drcaigou);
            }
            return dt;
        }
        #endregion
        #region 入库记录
        #region 上传图片
        public string UpFile(System.Web.UI.WebControls.FileUpload Fu_Raw_Poto)
        {
            string strMessage = string.Empty;
            string str = View_Operaction_UpFile.uploadexcel(Fu_Raw_Poto, out strMessage);
            string strFilePath = "/" + View_Operaction_UpFile.UpFileFolder + "/" + str;
            return System.Web.HttpContext.Current.Server.MapPath(strFilePath);
        }
        #endregion
        #region 将excel的数据显示出来
        public string ShowExcelInfo(DataTable dt)
        {
            StringBuilder sb = new StringBuilder();
            Int32 i = 0;

            if (dt != null && dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    Boolean bl = Int32.TryParse(dr[0].ToString(), out i);
                    if (bl)
                    {
                        sb.Append(" <tr>");

                        sb.Append("<td >" + dr[0] + "</td>");
                        sb.Append("<td >" + dr[1] + "</td>");
                        sb.Append("   <td >" + dr[2] + "</td>");
                        sb.Append("<td >" + dr[3] + "</td>");
                        sb.Append(" <td >" + dr[4] + "</td>");
                        sb.Append("<td >" + dr[5] + "</td>");
                        sb.Append("<td >" + dr[6] + "</td>");
                        sb.Append("<td > " + dr[7] + "</td>");
                        sb.Append("<td >" + dr[8] + "</td>");
                        sb.Append("<td >" + dr[9] + "</td>");
                        sb.Append("<td >" + dr[10] + "</td>");
                        sb.Append("<td > <input type=\"button\"  value=\"删除\" onclick=\"Del_row_Info(" + i + ")\"/></td>");
                        sb.Append("  </tr>");
                        i++;
                    }

                }
            }
            return sb.ToString();


        }
        #endregion
        #region 删除某条记录
        public DataTable Del_Excel_Out(string Del_Id, DataTable dt)
        {
            if (Del_Id != null)
            {

                if (dt != null)
                {
                    if (dt.Columns["id"] != null)
                    {
                        DataRow[] drs = dt.Select("id=" + Del_Id.Trim());
                        new Catering_Rules.InDbInfo().Delete(Convert.ToInt32(Del_Id));
                        dt.Rows.Remove(drs[0]);
                    }
                    else
                    {
                        DataRow[] dr_s = dt.Select("xuhao=" + Del_Id);

                        dt.Rows.Remove(dr_s[0]);
                    }
                }
                else
                {

                }
            }

            //  dt.Rows.Remove(dt.Rows[Convert.ToInt32(Del_Id)-1]);
            //     dt.Rows.RemoveAt(Convert.ToInt32(Del_Id));
            return dt;
        }
        #endregion
        #region 新增空行
        public string ShowHtml(Int32 i)
        {

            StringBuilder sb = new StringBuilder();
            sb.Append("<tr id=\"Add\">");
            sb.Append("<td><input type=\"checkbox\" id=\"ch_" + i + "\" name=\"ch_" + i + "\"/></td>");
            sb.Append("<td></td>");
            sb.Append("<td><input name=\"pinhao\" style=\"width:40px\" type=\"text\" value=\"\"/></td>");
            sb.Append("<td><input onfocus=\"calendar();\" name=\"danjuriqi\" style=\"width:70px\" type=\"text\"value=\"\"/></td>");
            sb.Append("<td><input name=\"pinming\" style=\"width:40px\" type=\"text\" value=\"\"/></td>");
            sb.Append("<td><input name=\"guige\" style=\"width:40px\" type=\"text\" value=\"\"/></td>");
            sb.Append("<td><input name=\"kushuliang\" style=\"width:70px\" type=\"text\" value=\"\"/></td>");
            sb.Append("<td><input name=\"danwei\" style=\"width:40px\" type=\"text\" value=\"\"/></td>");
            sb.Append("<td><input name=\"danjia\" style=\"width:40px\" type=\"text\" value=\"\"/></td>");
            sb.Append("<td><input name=\"bbxsje\" style=\"width:80px\" type=\"text\" value=\"\"/></td>");
            sb.Append("<td><input name=\"biliang\" style=\"width:40px\" type=\"text\" value=\"\"/></td>");
            sb.Append("<td><input name=\"beizhu\" style=\"width:100px\" type=\"text\" value=\"\"/></td>");
            sb.Append("<td>");
            sb.Append("<input type=\"button\"  value=\"保存\" onclick=\"but_Save_click()\"/>");
            sb.Append("<input type=\"button\"  value=\"删除\" onclick=\"Del_row_Info(" + i + ")\"/>");
            sb.Append("</td>");
            sb.Append("</tr>");
            return sb.ToString();
        }
        #endregion
        #region
        public DataTable Inset_Datatabl(DataTable dt, string InputInfo)
        {
            string[] strs = InputInfo.Replace("|", "").Split(new char[] { ',', ' ' });
            if (dt != null && dt.Rows.Count > 0)
            {
                if (strs.Length > 0)
                {
                    DataRow dr = dt.NewRow();
                    string strss = strs[1];
                    dr[0] = Convert.ToInt32(dt.Rows[dt.Rows.Count - 1][0]) + 1;
                    dr[1] = strs[1];
                    dr[2] = Convert.ToDateTime(strs[2]);
                    dr[3] = strs[3];
                    dr[4] = strs[4];
                    dr[5] = strs[5];
                    dr[6] = strs[6];
                    dr[7] = strs[7];
                    dr[8] = strs[8];
                    dr[9] = strs[9];
                    dr[10] = strs[10];
                    dt.Rows.Add(dr);

                }
            }
            else
            {
                dt = new Catering_DataSet.rukuxinxiDataTable();
                DataRow dr = dt.NewRow();

                dr[0] = 1;
                dr[1] = strs[1];
                dr[2] = Convert.ToDateTime(strs[2]);
                dr[3] = strs[3];
                dr[4] = strs[4];
                dr[5] = strs[5];
                dr[6] = strs[6];
                dr[7] = strs[7];
                dr[8] = strs[8];
                dr[9] = strs[9];
                dr[10] = strs[10];
                dt.Rows.Add(dr);

            }
            return dt;
        }
        public DataTable Inset_Datatabl(DataTable dt, Catering_Model.InDbInfo cmii, out Boolean reset)
        {
            Boolean bl = false;
            try
            {
                if (dt != null && dt.Rows.Count > 0 && cmii != null)
                {

                    DataRow dr = dt.NewRow();

                    dr[0] = Convert.ToInt32(dt.Rows[dt.Rows.Count - 1][0]) + 1;
                    dr[1] = cmii.RAWID;
                    dr[1] = cmii.GroupID;
                    dr[2] = cmii.ClassID;
                    dr[3] = cmii.DBID;
                    dr[4] = cmii.IntDate;
                    dr[5] = cmii.PiaoDate;
                    dr[6] = cmii.RAWID;
                    dr[7] = cmii.RAW;
                    dr[8] = cmii.Specifications;
                    dr[9] = cmii.Number;
                    dr[10] = cmii.CMP;

                    dr[11] = cmii.AmountMoney;
                    dr[12] = cmii.Weight;
                    dr[13] = cmii.RAWTID;
                    dr[14] = cmii.RAWIDS;
                    dr[15] = cmii.Back;
                    dr[16] = cmii.temp1;
                    dr[17] = cmii.temp2;
                    dt.Rows.Add(dr);


                }
                else
                {
                    DataSet ds = new Catering_Rules.InDbInfo().GetAllList();
                    if (ds != null && ds.Tables.Count > 0)
                    {
                        if (ds.Tables[0] != null)
                        {
                            dt = ds.Tables[0].Clone();
                            DataRow dr = dt.NewRow();

                            dr[0] = 1;

                            dr[1] = cmii.GroupID;
                            dr[2] = cmii.ClassID;
                            dr[3] = cmii.DBID;
                            dr[4] = cmii.IntDate;
                            dr[5] = cmii.PiaoDate;
                            dr[6] = cmii.RAWID;
                            dr[7] = cmii.RAW;
                            dr[8] = cmii.Specifications;
                            dr[9] = cmii.Number;
                            dr[10] = cmii.CMP;

                            dr[11] = cmii.AmountMoney;
                            dr[12] = cmii.Weight;
                            dr[13] = cmii.RAWTID;
                            dr[14] = cmii.RAWIDS;
                            dr[15] = cmii.Back;
                            dr[16] = cmii.temp1;
                            dr[17] = cmii.temp2;

                            dt.Rows.Add(dr);
                        }
                    }


                }
                bl = true;
            }
            catch
            {
            }
            reset = bl;
            return dt;
        }
        #endregion
        #endregion
        #region 库存台账
        private List<DateTime> GetDateList(string strMonth, string stryear)
        {
            List<DateTime> list = new List<DateTime>();
            string strdate = stryear + "-" + strMonth + "-01";
            string strWhere = string.Empty;
            DateTime dttime = Convert.ToDateTime(strdate);
            DateTime dtNext = dttime.AddDays(1 - dttime.Day).AddMonths(1).AddDays(-1);
            if (!strMonth.Equals("0") && !stryear.Equals("0"))
            {
                strWhere = " IntDate between '" + dttime.ToShortDateString() + "' and' " + dtNext.ToShortDateString() + "'";


            }
            DataSet ds = new Catering_Rules.ViewOperaction().GetViewToDataSet("Database_Out_Input_Time", strWhere);
            if (ds != null && ds.Tables.Count > 0)
            {
                if (ds.Tables[0].Rows.Count > 0)
                {
                    foreach (DataRow dr in ds.Tables[0].Rows)
                    {
                        if (dr[0] != null && dr[0].ToString().Length > 0)
                        {
                            DateTime tmepDate = Convert.ToDateTime(Convert.ToDateTime(dr[0].ToString()).ToShortDateString());
                            if (list.IndexOf(tmepDate) < 0)
                            {
                                list.Add(Convert.ToDateTime(tmepDate));
                            }
                        }
                    }
                }
            }
            return list;
        }
        private List<DateTime> GetDateList(string stryear)
        {
            List<DateTime> list = new List<DateTime>();
            string strdate = stryear + "-01-01";
            string strstop = stryear + "-12-31";
            string strWhere = string.Empty;


            if (!stryear.Equals("0"))
            {
                strWhere = " IntDate between '" + strdate + "' and' " + strstop + "'";


            }
            DataSet ds = new Catering_Rules.ViewOperaction().GetViewToDataSet("Database_Out_Input_Time", strWhere);
            if (ds != null && ds.Tables.Count > 0)
            {
                if (ds.Tables[0].Rows.Count > 0)
                {
                    foreach (DataRow dr in ds.Tables[0].Rows)
                    {
                        if (dr[0] != null && dr[0].ToString().Length > 0)
                        {
                            list.Add(Convert.ToDateTime(dr[0]));
                        }
                    }
                }
            }
            return list;
        }

        public DataTable GetTable(string GroupID, string ClassID, string DBID, string strRaw, string stryear, string strMoth)
        {
            List<DateTime> list = GetDateList(strMoth, stryear);
            return GetTaizhangDataTable(GroupID, ClassID, DBID, strRaw, list);
        }
        public DataTable GetTable(string GroupID, string ClassID, string DBID, string strRaw, string stryear)
        {
            List<DateTime> list = GetDateList(stryear);
            return GetTaizhangDataTable(GroupID, ClassID, DBID, strRaw, list);
        }
        public Catering_Model.RoomWaing GetWring(string GroupID, string ClassID, string DBID, string strRaw)
        {
            Catering_Rules.RoomWaing crrw = new Catering_Rules.RoomWaing();
            string strWhere = "GroupID=" + GroupID + "  and ClassID =" + ClassID + " and RoomID=" + DBID + " and RAWID=" + strRaw;
            DataSet ds = crrw.GetList(strWhere);
            Catering_Model.RoomWaing cmrw = new Catering_Model.RoomWaing();
            if (ds != null && ds.Tables.Count > 0)
            {
                if (ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
                {
                    DataRow dr = ds.Tables[0].Rows[0];
                    cmrw.GroupID = Convert.ToInt32(dr[1]);
                    cmrw.ClassID = Convert.ToInt32(dr[2]);
                    cmrw.RoomID = Convert.ToInt32(dr[3]);
                    cmrw.MinContent = Convert.ToInt32(dr[4]);
                    cmrw.MaxContent = Convert.ToInt32(dr[5]);
                    cmrw.temp1 = dr[6].ToString();
                    cmrw.temp2 = dr[7].ToString();
                }
            }
            return cmrw;
        }
        public Decimal Getjiecun(string GroupID, string ClassID, string DBID, string strRaw, DateTime dtime)
        {
            Decimal number = 0;
            string temp = new Catering_Rules.ViewOperaction().GetSingle_str("RawMaterial_Area", "InDBid", "RawID=" + strRaw);
            string str_In = "InDBid='" + temp + "' and GroupID=" + ClassID + "  and ClassID=" + GroupID + " and RawID=" + strRaw + " and IntDate < '" + dtime.AddDays(1) + "'";
            DataSet ds_in = new Catering_Rules.InDbInfo().GetList(str_In);
            string str_out = "InDBid='" + temp + "'and  GroupID=" + ClassID + "  and ClassID=" + GroupID + " and RawID=" + strRaw + " and OutDate < '" + dtime.AddDays(1) + "'";
            DataSet ds_out = new Catering_Rules.FromRecords().GetList(str_out);
            if (ds_in != null && ds_in.Tables.Count > 0 && ds_out != null && ds_out.Tables.Count > 0)
            {
                if (ds_in.Tables[0] != null && ds_in.Tables[0].Rows.Count > 0 && ds_out.Tables[0] != null && ds_out.Tables[0].Rows.Count > 0)
                {
                    decimal in_Number = 0, out_Number = 0;
                    foreach (DataRow dr in ds_in.Tables[0].Rows)
                    {
                        in_Number = in_Number + Convert.ToDecimal(dr["Number"]);
                    }
                    foreach (DataRow dr in ds_out.Tables[0].Rows)
                    {
                        out_Number = out_Number + Convert.ToDecimal(dr["Numbers"]);
                    }
                    number = in_Number - out_Number;
                }

            }
            return number;
        }
        private DataTable GetTaizhangDataTable(string GroupID, string ClassID, string DBID, string strRaw, List<DateTime> list)
        {
            DataSet ds_in = new Catering_Rules.InDbInfo().GetList("RAWID=" + strRaw);
            DataSet ds_out = new Catering_Rules.FromRecords().GetList("RAWID=" + strRaw);
            DataTable dt = new Catering_DataSet.kucuntaizhangDataTable();
            if (list.Count > 0)
            {

                foreach (DateTime dtime in list)
                {
                    DataRow dr = dt.NewRow();
                    dr["riqi"] = dtime.ToShortDateString();
                    dr["jiacharen"] = "";
                    dr["jiecun"] = Getjiecun(GroupID, ClassID, DBID, strRaw, dtime);

                    if (ds_in != null && ds_in.Tables.Count > 0)
                    {
                        if (ds_in.Tables[0] != null && ds_in.Tables[0].Rows.Count > 0)
                        {
                            var dsin = from dsins in ds_in.Tables[0].AsEnumerable()

                                       where dsins.Field<int>("GroupID") == Convert.ToInt32(GroupID)
                                       where dsins.Field<int>("ClassID") == Convert.ToInt32(ClassID)
                                       where dsins.Field<DateTime>("IntDate") >= dtime
                                       where dsins.Field<DateTime>("IntDate") <= dtime.AddDays(1)
                                       group dsins by dsins.Field<string>("InDBid") into dstemp
                                       select new
                                       {
                                           Name = dstemp.Key,
                                           Number = dstemp.Sum(o => Convert.ToDecimal(o.Field<string>("Number")))
                                       };
                            foreach (var dss in dsin)
                            {

                                Catering_Model.E_RawMaterial cmrm = new Catering_Rules.T_RawMaterial().GetModel(Convert.ToInt32(strRaw));
                                Boolean bl = Convert.ToInt32(GroupID) == (new Catering_Rules.ViewOperaction().GetSingle("RawMaterial_Area", "AreaID", "InDBid='" + dss.Name + "'"));
                                if (cmrm != null && bl)
                                {
                                    dr["danhao"] = dss.Name;
                                    dr["mingcheng"] = cmrm.RAWName;
                                    dr["ruku"] = dss.Number;
                                }
                            }

                        }

                    }
                    if (ds_out != null && ds_out.Tables.Count > 0)
                    {
                        if (ds_out.Tables[0] != null && ds_out.Tables[0].Rows.Count > 0)
                        {
                            var dsin = from dsins in ds_out.Tables[0].AsEnumerable()

                                       where dsins.Field<int>("GroupID") == Convert.ToInt32(GroupID)
                                       where dsins.Field<int>("ClassID") == Convert.ToInt32(ClassID)

                                       where dsins.Field<DateTime>("OutDate") >= dtime
                                       where dsins.Field<DateTime>("OutDate") <= dtime.AddDays(1)
                                       group dsins by dsins.Field<string>("InDBid") into dstemp
                                       select new
                                       {
                                           Name = dstemp.Key,
                                           Number = dstemp.Sum(o => Convert.ToDecimal(o.Field<string>("Numbers")))
                                       };
                            foreach (var dss in dsin)
                            {
                                Catering_Model.E_RawMaterial cmrm = new Catering_Rules.T_RawMaterial().GetModel(Convert.ToInt32(strRaw));
                                Boolean bl = Convert.ToInt32(GroupID) == (new Catering_Rules.ViewOperaction().GetSingle("RawMaterial_Area", "AreaID", "InDBid='" + dss.Name + "'"));
                                if (cmrm != null && bl)
                                {
                                    dr["danhao"] = dss.Name;
                                    dr["mingcheng"] = cmrm.RAWName;
                                    dr["chuku"] = dss.Number;
                                }
                            }

                        }

                    }
                    dt.Rows.Add(dr);
                }

            } return dt;
        }

        public string ShowTaizhang(DataTable dt)
        {
            StringBuilder sb = new StringBuilder();

            if (dt != null && dt.Rows.Count > 0)
            {
                int i = 0;
                foreach (DataRow dr in dt.Rows)
                {
                    if (dr != null)
                    {
                        DateTime dtime = Convert.ToDateTime(dr["riqi"]);
                        string strStyle = " class='tr01' onmouseover=\"this.className='tr03'\" onmouseout=\"this.className='tr01'\" ";
                        if (i % 2 == 1) strStyle = " class='tr02' onmouseover=\"this.className='tr03'\" onmouseout=\"this.className='tr02'\" ";
                        sb.Append(" <tr " + strStyle + ">");
                        sb.Append(" <td>" + dtime.Month + "</td>");
                        sb.Append(" <td>" + dtime.Day + "</td>");
                        sb.Append(" <td>" + dr["danhao"] + "</td>");
                        sb.Append(" <td>" + dr["mingcheng"] + "</td> ");
                        sb.Append("<td>" + dr["ruku"] + "</td>");
                        sb.Append("<td>" + dr["chuku"] + "</td>");
                        sb.Append(" <td>" + dr["jiecun"] + "</td>");
                        sb.Append("<td>" + dr["jiacharen"] + "</td>");
                        sb.Append(" </tr>");
                        i++;
                    }
                }


            }

            return sb.ToString();
        }
        #endregion


    }
}